package com.wuyou.mid;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 2090. 半径为 k 的子数组平均值
 *
 * @Date 2024/12/4 10:11
 * @Author 无忧
 */
public class GetAverages {

    public static void main(String[] args) throws IOException {
//        File file = new File("D:\\workspace_IDEA\\leetcode\\src\\main\\java\\com\\wuyou\\mid\\case\\GetAveragesCase34.txt");
//        BufferedReader br = new BufferedReader(new FileReader(file));
//        StringBuilder sb = new StringBuilder();
//        String line;
//        while ((line = br.readLine()) != null) {
//            sb.append(line);
//        }
//        String[] split = sb.toString().split(",");
//        List<Integer> list = new ArrayList<>();
//        for (int i = 0; i < split.length; i++) {
//            list.add(Integer.valueOf(split[i]));
//        }
//        System.out.println(split.length);
//        Integer[] integers = list.toArray(nums);
        Integer[] nums = {1,11,17,21,29};
        int k = 4;
        int[] res = sol_1(nums, k);

        for (int i : res) {
            System.out.print(i + "\t");
        }
    }

    public static int[] sol_1(Integer[] nums, int k) {
        int length = nums.length, l = 0, r = 0, radius = (k * 2) + 1, idx = 0;
        long sum = 0;
        int[] res = new int[nums.length];
        // 计算半径总和
        if (length < radius) {
            while (r < length){
                res[r++] = -1;
            }
            return res;
        }
        while (r < length && idx < radius) {
            if (r < k) {
                res[r++] = -1;
            }
            sum += nums[idx++];
        }
        while (r < length - k) {
            res[r++] = (int) (sum / radius);
            if (idx < length) {
                sum = sum - nums[l++] + nums[idx++];
            }
        }
        while (r < length) {
            res[r++] = -1;
        }
        return res;
    }
}
